import 'package:flutter/material.dart';
import 'package:maiya_assistant/page/appointment_page.dart';
import 'package:maiya_assistant/page/me_page.dart';
import 'package:maiya_assistant/page/message_page.dart';
import 'package:maiya_assistant/page/note_page.dart';
import 'package:maiya_assistant/page/user_management_page.dart';

class HomePage extends StatefulWidget {
  const HomePage({Key? key}) : super(key: key);

  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  int _index = 0;

  final List<Widget> _homeWidgets = [
    const AppointmentPage(),
    const MessagePage(),
    const NotePage(),
    const UserManagementPage(),
    const MePage()
  ];

  void _onBottomNavigationBarTapped(index) {
    setState(() {
      _index = index;
    });
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      body: IndexedStack(
        index: _index,
        children: _homeWidgets,
      ),
      bottomNavigationBar: BottomNavigationBar(
        type: BottomNavigationBarType.fixed,
        currentIndex: _index,
        onTap: _onBottomNavigationBarTapped,
        selectedItemColor: Theme.of(context).primaryColor,
        items: [
          _getBottomNavItem(
              '预约', 'images/appo.png', 'images/appo_selected.png', 0),
          _getBottomNavItem(
              '消息', 'images/message.png', 'images/message_selected.png', 1),
          _getBottomNavItem(
              '备忘录', 'images/note.png', 'images/note_selected.png', 2),
          _getBottomNavItem(
              '用户', 'images/user.png', 'images/user_selected.png', 3),
          _getBottomNavItem(
              '我的', 'images/me.png', 'images/me_selected.png', 4),
        ],
      ),
    );
  }

  BottomNavigationBarItem _getBottomNavItem(
      String title, String normalIcon, String pressedIcon, int index) {
    return BottomNavigationBarItem(
      icon: _index == index
          ? Image.asset(
              pressedIcon,
              width: 24,
              height: 24,
            )
          : Image.asset(
              normalIcon,
              width: 24,
              height: 24,
            ),
      label: title,
    );
  }
}
